package com.sc;

import org.junit.Test;

import java.util.*;

/**
 * Created by Shen Chao on 2018-05-31-031.
 */
public class MapDemoTest {

    @Test
    public void map() {
        Map<String, String> map = new HashMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");
        for (Iterator iterator = map.values().iterator(); iterator.hasNext(); ) {
            String name = (String) iterator.next();
            System.out.println(name);
        }
    }

    @Test
    public void map1() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");
        for (Iterator iterator = map.values().iterator(); iterator.hasNext(); ) {
            String name = (String) iterator.next();
            System.out.println(name);
        }
    }

    @Test
    public void mapBianli1() {
        Map<String, String> map = new HashMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");

        for (String key : map.keySet()) {
            System.out.println("key = " + key + ";value = " + map.get(key));
        }
    }

    @Test
    public void mapBianli2() {
        Map<String, String> map = new HashMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");

        for (String value : map.values()) {
            System.out.println("value = " + value);
        }
    }

    @Test
    public void mapBianli3() {
        Map<String, String> map = new TreeMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");

        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key = " + entry.getKey() + ";value = " + entry.getValue());
        }
    }

    @Test
    public void mapBianli4() {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("a3", "aa");
        map.put("a2", "bb");
        map.put("b1", "cc");
        map.put("a1", "dd");
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            System.out.println("key = " + entry.getKey() + ";value = " + entry.getValue());
        }
    }

    @Test
    public void parseInt() {
        System.out.println(Integer.parseInt("0001111", 2));
        System.out.println(Integer.parseInt("0011111", 2));
        System.out.println(Integer.parseInt("0111111", 2));
        System.out.println(Integer.parseInt("1111111", 2));
        System.out.println(Integer.parseInt("0001111", 2) & 15);
        System.out.println(Integer.parseInt("0011111", 2) & 15);
        System.out.println(Integer.parseInt("0111111", 2) & 15);
        System.out.println(Integer.parseInt("1111111", 2) & 15);

    }

    @Test
    public void listTest() {
        System.out.println("ArrayList添加" + N + "条耗时：" + timeList(new ArrayList()));
        System.out.println("LinkedList添加" + N + "条耗时：" + timeList(new LinkedList()));

        List list1 = addList(new ArrayList<>());
        List list2 = addList(new LinkedList<>());
        System.out.println("ArrayList查找" + N + "条耗时：" + readList(list1));
        System.out.println("LinkedList查找" + N + "条耗时：" + timeList(list2));
    }

    final int N = 100000;

    public long timeList(List list) {
        long start = System.currentTimeMillis();
        Object o = new Object();
        for (int i = 0; i < N; i++) {
            list.add(0, o);
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public long readList(List list) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
        }
        long end = System.currentTimeMillis();
        return end - start;
    }

    public List addList(List list) {
        Object o = new Object();        for (int i = 0; i < N; i++) {
            list.add(0, o);
        }
        return list;
    }
}